<?php

//
session_start();
//
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {

    header("Location: login.php");
}
/*if (isset($_SESSION['Data'])) {
    print "<br>Form.php - session data is set :";
    $xxx = $_SESSION['Data'];
    var_dump($xxx);
} else {
    print "<br>Form.php - no SESSION Data";
}*/
//
include 'i_functions.php';
//
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    //var_dump($_GET);
    $DataType = $_SESSION['DataType'];
    //$_SESSION['Trace'] = "Y";
    $Trace = $_SESSION['Trace'];
    $FormID = $_GET['Form'];
    if (!$FormID) {
        $FormID = "You need to give me a form ID to start!";
    }
    //$ParamID = $FormID . ".txt";
    $ParamID = "params/" . $FormID . ".txt";
    //print "<br>" . $ParamID;
    $FormTitle = $_GET['Title'];
    if (!$FormTitle) {
        $FormTitle = $FormID;
    }
    $TableName = $_GET['Table'];
    //$DataType = $_GET['DataType'];
    //$Trace = $_GET['Trace'];
    $ID = $_GET['ID'];
    //
    /*$_SESSION['Work'] = array();
    $strWork = $_GET['Work'];
    $tmpWork = explode("|", $strWork);
    //print "<br>Form.php: tmpWork = ";
    //var_dump($tmpWork);
    $wcount = count($tmpWork);
    for ($w = 0; $w < $wcount; $w++) {
        $tmp = explode(":", $tmpWork[$w]);
        $KeyName = $tmp[0];
        $KeyVal = $tmp[1];
        $_SESSION['Work'][$KeyName] = $KeyVal;
    }*/
    if ($Trace) {
        print "<br>Form.php - " . $FormID . "<br>Work=";
        var_dump($_SESSION['Work']);
        print "<br>ID = " . $ID;
        print "<br>TableName = " . $TableName;
    }
    //
    $UE_Opts['Exit'] = "A";
    //$UE_Opts['Trace'] = true;
    $UE_Func = $FormID;
    $UE_Code = $UE_Func . ".php";
    $ParamRec = "";
    $Data = "";
    $q = 0;
    //print $UE_Code . "<br>";
    if (is_file($UE_Code)) {
        include $UE_Code;
        //$_SESSION['Work']['ID'] = $ID;
        $Val = $UE_Func($ParamRec, $Data, $q, $UE_Opts);
    }
//             <input name=\"DataType\" type=\"hidden\" id=\"DataType\" value=\"$DataType\"/>
    //
    $HTML_B = "<br><div><form name=\"$FormID\" method=\"post\" action=\"FormUpdate.php\">
            <input name=\"FormID\" type=\"hidden\" id=\"FormID\" value=\"$FormID\"/>
            <input name=\"TableName\" type=\"hidden\" id=\"TableName\" value=\"$TableName\"/> 
            <input name=\"ID\" type=\"hidden\" id=\"ID\" value=\"$ID\"/>
            <table id=\"form\" align=\"center\" cellspacing=\"0\"cellpadding=\"0\">
            <tr class=\"col_head\"><td >&nbsp;</td><td><h1>$FormTitle</h1></td></tr>";
    $HTML_T = "<tr><td></td><td><br><input id=\"btnsubmit\" name=\"btnsubmit\" type=\"submit\" value=\"Submit\" onClick=\"return(valid(document.frmclaim.txttele));\" />
            </td></tr></table></form></div>";
//
    $user_name = "root";
    $pass_word = "haddons";
    $database = "PPI";
    $server = "127.0.0.1";
//
    $db_handle = mysql_connect($server, $user_name, $pass_word);
    $db_found = mysql_select_db($database, $db_handle);
//
    $Options = array();
    $Options['TableName'] = $TableName;
    $Options['DataType'] = $DataType;
    $Options['Trace'] = $Trace;
    $Options['ID'] = $ID;
    $HTML_F = BuildForm($FormID, $Options);
    //
    $strWork = "";
    if (!empty($_SESSION['Work'])) {
        foreach ($_SESSION['Work'] as $KeyName => $KeyValue) {
            if ($strWork != "") {
                $strWork .= "|";
            }
            $strWork .= $KeyName . ":" . $KeyValue;
        }
        //print "<br> Form.php strWork = " . $strWork;
    }
    include 'i_head.php';
    print $HTML_B . $HTML_F . $HTML_T;
    include 'i_foot.html';
}
?>